package com.njh.tools.common.phone;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;

import java.util.List;

/**
 * @Description:这个工具类主要通过显式意图来跳转到指定Activity
 * @163.com,然后我再添加到这个工具类里面,分享给大家.
 */
public class ToActivityUtil {
    private static ToActivityUtil mToActivityUtil;
    public static int REQUESTCODE=10000;
    public static int RESUTLCODE=10001;
    public static int RESUTLCODE1=10002;
    public static ToActivityUtil getInstance() {
        return null == mToActivityUtil ? mToActivityUtil = new ToActivityUtil() : mToActivityUtil;
    }

    /**
     * @param packageContext
     * @param action         含操作的Intent
     * @Description: 隐式启动, 跳转
     */
    public void startActivityIntentSafe(Context packageContext,
                                        Intent action) {
        // Verify it resolves
        PackageManager packageManager = packageContext.getPackageManager();
        List activities = packageManager.queryIntentActivities(action,
                PackageManager.MATCH_DEFAULT_ONLY);
        boolean isIntentSafe = activities.size() > 0;

        // Start an activity if it's safe
        if (isIntentSafe) {
            packageContext.startActivity(action);
        }

    }

    /**
     * @param packageContext
     * @param cls
     * @param keys
     * @param values         手动改变int[] values类型,可以传递其它数据类型,就不重载了
     * @Description: 跳转, 带参数的方法;需要其它的数据类型,再继续重载吧,暂时只写这么多吧,意义不大
     */
    public void toNextActivity(Context packageContext, Class<?> cls,
                               String[] keys, int[] values) {
        Intent i = new Intent(packageContext, cls);
        for (int j = 0; j < values.length; j++) {
            i.putExtra(keys[j], values[j]);
        }
        packageContext.startActivity(i);

    }

    /**
     * @param packageContext from,一般传XXXActivity.this
     * @param cls            to,一般传XXXActivity.class
     * @Description: 跳转
     */
    public void toNextActivity(Context packageContext, Class<?> cls) {
        Intent i = new Intent(packageContext, cls);
        packageContext.startActivity(i);
    }
    /**
     * @param packageContext
     * @param cls
     * @param keyvalues      需要传进去的String参数{{key1,values},{key2,value2}...}
     * @Description: 跳转, 带参数的方法;需要其它的数据类型,再继续重载吧
     */
    public void toNextActivity(Context packageContext, Class<?> cls,
                               String[][] keyvalues) {
        Intent i = new Intent(packageContext, cls);
        for (String[] strings : keyvalues) {
            i.putExtra(strings[0], strings[1]);
        }
        packageContext.startActivity(i);

    }

    /**
     * @param packageContext
     * @param cls
     * @Description: 跳转, 带参数的方法;需要其它的数据类型,再继续重载吧
     */
    public void toNextActivity(Context packageContext, Class<?> cls,
                               Bundle bundle) {
        Intent i = new Intent(packageContext, cls);
        i.putExtras(bundle);
        packageContext.startActivity(i);

    }

    /**跳转activity*/
    public void toNext(Context packageContext, Class<?> clazz) {
        toNext(packageContext,clazz, null);
    }

    /**跳转activity,带有bundle数据的*/
    public void toNext(Context packageContext, Class<?> clazz, Bundle data) {
        Intent intent = new Intent(packageContext, clazz);
        if(data != null) {
            intent.putExtras(data);
        }
        packageContext.startActivity(intent);
    }

    /**跳转activity,带有bundle数据的*/
    public void toNextAddTags(Context packageContext, Class<?> clazz, Bundle data, int[] tags) {
        Intent intent = new Intent(packageContext, clazz);
        if(tags != null && tags.length > 0) {
            for (int i = 0; i < tags.length; i++) {
                intent.addFlags(tags[i]);
            }
        }
        if(data != null) {
            intent.putExtras(data);
        }
        packageContext.startActivity(intent);
    }

    public void finish(Activity activity) {
        activity.finish();
    }
}